/*!
 * Copyright (c) 2010 Jacob Essex
 *
 * Dual licenced under MIT or GPL 3 licneces
 * 
 * See the GPL3 or MIT files in the main directory for details
 */

require.def('global/inject',['config', 'se/site', 'lib/jquery'], 
    function(Config, Se ){
        var site = new Se.Site();

        if ( site.user() === Se.NO_USER ){ 
            return;
        }

        var getTabHash = function(){
            return 'host=' + window.location.host 
                    + '&uid=' + site.user()
                    + '&css=' + encodeURIComponent(site.css()) //TODO only supports one css file 
        };

        //we always want to show the stacked odds options as it allows us to add a site
        //however we only want to add do it if the user is the logged in and on their user page
        (function(){
            var url = window.location.toString(); //http://stackoverflow.com/users/118145/yacoby 
            var match = /\/users\/([0-9]+)/.exec(url);
            if ( match ){
                var currentUsr = match.length > 1 ? match[1] : Se.NO_USER;
                if ( site.user() == currentUsr ){
                    site.options.addTab(
                        Config.OptionsTabName,
                        chrome.extension.getURL('pages/user/options.html#' + getTabHash())
                    );
                }
            }
        })();

        //Event hanlder. Used for passing data
        $('body').append('<div id="so-msg" style="display:none"></div>');

        function createHandler(evt, callback){
            $('#so-msg').append('<span id="' + evt + '"></span>');
            //$('#so-msg #' + evt).bind(evt, function() {
            document.getElementById(evt).addEventListener(evt, function(){
                var arg = $('#'+evt + ' .arg').html();
                var callbackName = $('#'+evt + ' .callback').html();
                callback(arg, function(){
                    $('body').append('<script>window.' + callbackName + '()</script>');
                });
            });
        }

        createHandler('setSiteActive', function(host, callback){
            var req = {
                obj:'site',
                db: Config.SettingsDb
            };
            req.func = 'getByHost';
            req.args = [host];

            chrome.extension.sendRequest(req, function(siteInfo){
                    siteInfo.IsActive = 1;

                    req.func = 'saveSite';
                    req.args = [siteInfo];
                    chrome.extension.sendRequest(req, callback);
            });
            
        });



        var req = {
            obj:'site',
            db: Config.SettingsDb
        }

        req.func = 'getByHost';
        req.args = [window.location.host];

        chrome.extension.sendRequest(req, function(siteInfo){
            if ( siteInfo.IsActive == 1 ){
                site.unanswered.addTab(
                        Config.TabName,
                        chrome.extension.getURL('pages/unanswered/page.html#' + getTabHash())
                ); 
                site.questions.addTab(
                        Config.TabName,
                        chrome.extension.getURL('pages/questions/page.html#' + getTabHash())
                ); 
            }else if ( siteInfo.HasNotified == 0 ){
                //I can't seem to pass functions, so we have to pass an object to the saveSite function
                var setNotified = function(){
                    req.func = 'saveSite';
                    siteInfo.HasNotified = 1;
                    req.args = [siteInfo];
                    chrome.extension.sendRequest(req);
                }

                site.addNotification('Do you want to <a href="#">enable stacked odds</a> for this site',{
                    yes:function(){
                        site.options.gotoTab(Config.OptionsTabName);
                        setNotified();
                    },
                    no: setNotified
                });
                
            }
            site.runTabCheck();
        });
    }
);


